  import { JSEncrypt, CryptoJS } from '@/plugins'
  import {
    FRONT_PUB_KEY,
    FRONT_PRIVATE_KEY,
    SERVER_PUB_KEY,
    SERVER_PRIVATE_KEY
  } from '@/configs';
  
  // 对称加密
  export const aesEncrypt = (msg, aesKey) => {
    return CryptoJS.AES.encrypt(msg, aesKey, {
      mode: CryptoJS.mode.CBC, 
      padding: CryptoJS.pad.Pkcs7
    }).toString();
  }
  // 对称解密
  export const aesDecrypt = (encryptedData, aesKey) => {
    return CryptoJS.AES.decrypt(encryptedData, aesKey, {
      mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7
    }).toString(CryptoJS.enc.Utf8)
  }
	
	// RSA加密
	export const rsaEncrypt = (data) => {
    let srcData = null
    if (typeof(data) == 'string') {
      srcData = data
    } else {
      srcData = JSON.stringify(data)
    }
    const crypt = new JSEncrypt();
    crypt.setPublicKey(FRONT_PUB_KEY)
    // crypt.setPrivateKey(FRONT_PRIVATE_KEY)  // 错误用法
    return crypt.encrypt(srcData)
	}
	
  // RSA解密
	export const rsaDecrypt = (data) => {
	  const crypt = new JSEncrypt()
	  // crypt.setPublicKey(FRONT_PUB_KEY)
	  crypt.setPrivateKey(FRONT_PRIVATE_KEY)
		const deCode = crypt.decrypt(data)
	  try {
	    return JSON.parse(deCode)
	  } catch (err) {
	    return deCode
	  }
	}

  